package com.elective.courseselect.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.TeacherDao;
import com.elective.courseselect.entity.AnswerEntity;
import com.elective.courseselect.entity.CourseEntity;
import com.elective.courseselect.entity.TeacherEntity;
import com.elective.courseselect.entity.UserEntity;
import com.elective.courseselect.enums.CourseStatus;
import com.elective.courseselect.service.*;
import com.elective.courseselect.serviceUtils.CourseUtils;
import com.elective.courseselect.serviceUtils.UserUtils;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Autowired
    UserService userService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseCharacterService courseCharacterService;

    @Autowired
    CharacterService characterService;

    @Autowired
    AnswerService answerService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public TeacherEntity searchTeacherInfos(Integer userId) {
        QueryWrapper<TeacherEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        TeacherEntity teacherEntity = this.getOne(wrapper);
        return teacherEntity;
    }

    @Override
    public void updateTeacherInfo(TeacherEntity teacher) {
        QueryWrapper<TeacherEntity> teacherEntityQueryWrapper = new QueryWrapper<>();
        teacherEntityQueryWrapper.eq("user_id", teacher.getUserId());
        this.update(teacher,teacherEntityQueryWrapper);

        //修改密码
        Integer userId = teacher.getUserId();
        String password = teacher.getPassword();
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("id", userId);
        UserEntity userEntity = userService.getOne(userEntityQueryWrapper);
        userEntity.setPassword(password);
        userService.update(userEntity,userEntityQueryWrapper);

    }

    @Override
    public List<CourseEntity> searchTeacherCourse(Integer userId) {
        QueryWrapper<CourseEntity> courseEntityQueryWrapper = new QueryWrapper<>();
        courseEntityQueryWrapper.eq("teacher_id", userId);
        List<CourseEntity> list = courseService.list(courseEntityQueryWrapper);

        for (int i = 0; i < list.size(); i++) {
            CourseEntity courseEntity = list.get(i);
            //过滤用户不正常的
            Boolean abandon = UserUtils.isAbandon(courseEntity.getTeacherId());
            if (abandon) {
                list.remove(i);
                continue;
            }
            //过滤掉禁用的课程,老师可看修改中,正常,驳回的课程
            if (courseEntity.getStatus() == CourseStatus.COURSE_FORBID) {
                list.remove(i);
                continue;
            }
            CourseUtils.getCourseLabels(courseEntity);
            courseEntity.setScheduleByJson((JSONObject) JSONObject.parse(courseEntity.getSchedule()));
        }
        return list;
    }

    @Override
    public void teacherJudgeObj(Map<String, Object> map) {
        Integer questionnaireId = (Integer) map.get("questionnaireId");
        Integer userId = (Integer) map.get("userId");

        //查询出之前学生回答此套问卷的数据
        QueryWrapper<AnswerEntity> answerEntityQueryWrapper = new QueryWrapper<>();
        answerEntityQueryWrapper.eq("student_id", userId);
        answerEntityQueryWrapper.eq("questionnaire_id", questionnaireId);
        //学生历史填写答案
        AnswerEntity answerEntity = answerService.getOne(answerEntityQueryWrapper);
        JSONObject StuSolution = JSONObject.parseObject(answerEntity.getSolution());
        JSONArray solutionArray = StuSolution.getJSONArray("solution");

        //老师传递过来的主观题obj
        ArrayList objects = (ArrayList) map.get("objects");
        for (Object object : objects) {
            HashMap<String,Integer> subjective = (HashMap) object;
            int objectId = subjective.get("objectId");
            int objectVal = subjective.get("objectVal");
            for (Object o : solutionArray) {
                JSONObject oByJson = (JSONObject) o;
                if (oByJson.getInteger("questionId") == objectId) {
                    oByJson.put("isCorrect", true);
                    oByJson.put("stuScore", objectVal);
                }
            }
        }
        StuSolution.put("solution", solutionArray);

        //重新计算总分
        Integer totalScore = 0;
        for (Object o : solutionArray) {
            JSONObject jsonObject = JSONObject.parseObject(o.toString());
            Integer stuScore = jsonObject.getInteger("stuScore");
            totalScore += stuScore;
        }
        answerEntity.setTotalScore(totalScore);
        //给学生输入的主观题赋值分数
        answerEntity.setSolution(StuSolution.toString());
        answerService.update(answerEntity, answerEntityQueryWrapper);
    }


}